package cc.xsubj.trading.entity;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.geom.Rectangle;

import cc.xsubj.engine.Globals;
import cc.xsubj.engine.ResourceManager;
import cc.xsubj.engine.entity.Entity;
import cc.xsubj.trading.inventory.Inventory;
import cc.xsubj.trading.inventory.InventorySlot;
import cc.xsubj.trading.inventory.ItemStack;
import cc.xsubj.trading.inventory.ItemType;
import cc.xsubj.trading.inventory.ItemRegistry;
import cc.xsubj.trading.messages.SharpText;
import cc.xsubj.trading.world.InventoryWorld;

public class InventoryItem extends Entity
{
    private static final int PADDING_X = 5;
    private float oldx, oldy;
    private static TrueTypeFont nameFont = new TrueTypeFont(new java.awt.Font("SansSerif", java.awt.Font.PLAIN, 9), false);
    
    private ItemStack itemStack;
    private boolean beingDragged;
    
    private boolean inPlayerInventory;
    
    public InventorySlot slot;

    public InventoryItem(float x, float y, ItemStack stack)
    {
        super(x, y);
        SpriteSheet items = ResourceManager.getSpriteSheet("items_sheet");
        items.setFilter(Image.FILTER_NEAREST);
        setGraphic(items);
        
        int itemID = ItemRegistry.getIDByItem(stack.getType());
        
        addAnimation("ITEM", false, itemID/items.getHorizontalCount(), itemID%items.getVerticalCount());
        this.width = 24;
        this.height = 24;
        
        itemStack = stack;
        
        this.addType("InventoryItem");
    }
    
    @Override
    public void render(GameContainer container, Graphics g)
            throws SlickException
    {
        super.render(container, g);
        
        float mouseX = container.getInput().getMouseX() / Globals.scaleX;
        float mouseY = container.getInput().getMouseY() / Globals.scaleY;
        
        String quantityString = "x" + itemStack.getQuantity();
        
        if(containsPoint((int)mouseX, (int)mouseY))
        {
            String text = itemStack.getName();
            if(itemStack.getQuantity() > 1)
            {
                text += " " + quantityString;
            }
            SharpText.drawString(text, g, this.x + this.width + PADDING_X, this.y);
            int value;
            if (isInPlayerInventory())
            {
                value = Inventory.getNPCInventory(InventoryWorld.npcName).getWillBuyPrice(itemStack.getType());
            } else {
                value = Inventory.getNPCInventory(InventoryWorld.npcName).getWillSellPrice(itemStack.getType());
            }
            SharpText.drawString(""+itemStack.getQuantity() * value + "G", g, this.x + this.width + PADDING_X, this.y + 10);
        } else {
            SharpText.drawString(quantityString, g, this.x, this.y - 3);
        }
    }

    public boolean containsPoint(int x, int y)
    {
        return (x >= this.x && 
                x <= this.x+this.width &&
                y >= this.y &&
                y <= this.y+this.height);
    }
    
    public void updateOldPosition()
    {
        oldx = x;
        oldy = y;
    }
    
    public void restoreOldPosition()
    {
//        x = oldx;
//        y = oldy;
        x = slot.getBounds().getX();
        y = slot.getBounds().getY();
    }
    
    public void setNewPosition(float x, float y)
    {
        oldx = x;
        oldy = y;
        this.x = x;
        this.y = y;
    }

    public boolean isBeingDragged()
    {
        return beingDragged;
    }

    public void setBeingDragged(boolean beingDragged)
    {
        this.beingDragged = beingDragged;
    }
    
    public ItemStack getItemStack()
    {
        return itemStack;
    }

    public boolean isInPlayerInventory()
    {
        return inPlayerInventory;
    }

    public void setInPlayerInventory(boolean inPlayerInventory)
    {
        this.inPlayerInventory = inPlayerInventory;
    }

}
